package com.game;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Bullet extends GameObject{
    //子弹大小
    private int width = 10;
    private int height = 10;
    //子弹速度
    private int speed = 7;
    //子弹方向
    Direction direction;

    public Bullet(String img, int x, int y, GamePanel gamePanel, Direction direction) {
        super(img, x, y, gamePanel);
        this.direction = direction;
    }

    /*
    * 子弹移动
    * */
    public void shoot() {
        switch (direction) {
            case LEFT:
                x -= speed;
                break;
            case RIGHT:
                x += speed;
                break;
            case UP:
                y -= speed;
                break;
            case DOWN:
                y += speed;
                break;
        }
        //将子弹与围墙的碰撞检测 加到子弹移动方法中
        this.hitWall();
        this.hitFe();
        //将子弹出界删除 加到子弹移动方法中
        this.moveToBorder();
        //将子弹与基地的碰撞检测 加到子弹移动方法中
        this.hitBase();
    }

    /*
    * 我方子弹撞击敌方坦克
    * */
    public void hitBot(){
        ArrayList<Bot> bots = this.getGamePanel().botlist;
        for (Bot bot : bots) {
            if (this.gerRec().intersects(bot.gerRec())){
                //添加爆炸动画的位置
                getGamePanel().explodeList.add(new Explode("image/cxkboom.gif", bot.x,bot.y,getGamePanel()));
                this.getGamePanel().botlist.remove(bot);
                this.getGamePanel().removeList.add(this);
                Music.explodePlay();
                break;
            }
        }
    }

    /*
    * 子弹与子弹的检测碰撞
    * */
    public void hitBullet(){
        Rectangle next = this.gerRec();
        ArrayList<EnemyBullet> enemyBullets = getGamePanel().enemyBulletsList;
        for (EnemyBullet enemyBullet : enemyBullets) {
            if (enemyBullet.gerRec().intersects(next)){
                //删除子弹
                getGamePanel().removeList.add(this);
                getGamePanel().removeList2.add(enemyBullet);
                Music.wallPlay();
                break;
            }
        }
    }

    /*
    * 子弹和围墙的检测碰撞
    * */
    public void hitWall(){
        //初始化围墙列表
        ArrayList<Wall> wallsList = getGamePanel().wallList;
        for (Wall wall : wallsList) {
            if (this.gerRec().intersects(wall.gerRec())){
                //删去围墙
                this.getGamePanel().wallList.remove(wall);
                //删去子弹
                this.getGamePanel().removeList.add(this);
                //停止循环
                Music.wallPlay();
                break;
            }
        }
    }

    /*
    * 子弹与基地碰撞检测
    * */
    public void hitBase() {
        ArrayList<Base> bases = getGamePanel().baseList;
        for (Base base : bases) {
            if (this.gerRec().intersects(base.gerRec())) {
                getGamePanel().baseList.remove(base);
                getGamePanel().removeList.add(this);
                getGamePanel().explodeList.add(new Explode("image/cxkboom.gif", base.x+9, base.y, this.getGamePanel()));
                Music.hitBasePlay();
                break;
            }
        }
    }

    /*
    * 子弹出界后删除，减小内存消耗
    * */
    public void moveToBorder(){
        if (x < 0 || x + width > getGamePanel().width){
            getGamePanel().removeList.add(this);
        }
        else if (y < 0 || y + height > getGamePanel().height){
            getGamePanel().removeList.add(this);
        }
    }

    /*
     * 子弹与Fe
     */
    public void hitFe() {
        Rectangle next = this.gerRec();
        ArrayList<Fe> fes = getGamePanel().feList;
        for (Fe fe : fes) {
            if (fe.gerRec().intersects(next)) {
                Music.wallPlay();
                getGamePanel().removeList.add(this);
                break;
            }
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(getImg(),x,y,this.width,this.height,null);
        //在绘制方法中加入子弹移动
        this.shoot();
        this.hitBot();
        //子弹与子弹的检测碰撞
        this.hitBullet();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,this.width,this.height);
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

}
